package com.xjprojectc.business.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.xjprojectc.base.action.SysBaseAction;
import com.xjprojectc.business.pojo.T_agentaccountinfo;
import com.xjprojectc.business.pojo.T_prepaidcard;
import com.xjprojectc.business.service.AgentaccountinfoService;
import com.xjprojectc.business.service.PrepaidcardService;
import com.xjprojectc.uitl.JsonUtil;
import com.xjprojectc.uitl.Page;
import com.xjprojectc.webservice.Call;


/**
 * 
 * 
 * 充值卡管理
 * 
 * @author Administrator
 * 
 */

@SuppressWarnings("serial")
public class PrepaidcardAction extends SysBaseAction {
	private PrepaidcardService prepaidcardService;
	private T_prepaidcard prepaidcard;
	private List<T_prepaidcard> prepaidcardList;
	private AgentaccountinfoService agentaccountinfoService;
	private List<T_agentaccountinfo> agentaccountinfoList;
	private String errorMsg;
	private int parame;
	private String msg;
	private String id;
	
	

	/**
	 * 
	 * 列表页面数据处理
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String list() {
		if(prepaidcard == null){
			prepaidcard = new T_prepaidcard();
		}
		T_agentaccountinfo user = this.getSysUser();
		prepaidcard.setCreateuser(user.getUsername());
		
		List<T_prepaidcard>  list = prepaidcardService.getPrepaidcardByCount(prepaidcard);
		if(list.size()>0){
			T_prepaidcard card = list.get(0);
			prepaidcard.setCard_num(card.getCard_num());
			prepaidcard.setCard_money(card.getCard_money());
		}else{
			prepaidcard.setCard_num("0");
			prepaidcard.setCard_money("0.00");
		}
		

		int firstResult = (currentPage - 1) * pageSize;
		int maxResults = pageSize;
		Page page = new Page(prepaidcardService.getPageCount(prepaidcard));
		page.setCurrentPage(firstResult / maxResults + 1);
		prepaidcard.setStart_row(page.getStartRow());
		prepaidcard.setEnd_row(page.getStartRow() + pageSize);
		super.setPageInfo(page);
		prepaidcardList = prepaidcardService.getPrepaidcardByPager(prepaidcard);
		
		
		T_agentaccountinfo agent = new T_agentaccountinfo();
		agent.setField4(user.getUsername());
		agentaccountinfoList = agentaccountinfoService.getAgentaccountinfoByList(agent);
		
		return "list_view";
	}


	/**
	 * 
	 * 跳转到新增页面
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String toAdd() {
		return "add_view";
	}

	/**
	 * 增加
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String add() {
		T_agentaccountinfo user = this.getSysUser();
		prepaidcardList = new ArrayList<T_prepaidcard>();
		String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		int num = Integer.parseInt(prepaidcard.getCardnumber());
		Float cardmoney = prepaidcard.getCardmoney();
		for(int i=0;i<num;i++){
			prepaidcard = new T_prepaidcard();
			prepaidcard.setCreatedate(time);
			prepaidcard.setCreateuser(user.getUsername());
			prepaidcard.setCardstatus(0);
			prepaidcard.setCardunit(2);
			prepaidcard.setCardmoney(cardmoney);
			prepaidcard.setCardnumber("czk"+java.util.UUID.randomUUID().toString().replaceAll("-", ""));
			prepaidcard.setCardpwd(java.util.UUID.randomUUID().toString().replaceAll("-", ""));
			prepaidcard.setLogid(this.getLogid(user));
			prepaidcardList.add(prepaidcard);
		}
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("user", Call.setCallUser(user));
			map.put("data", JsonUtil.jsonArrayDelEmpty(JSONArray.fromObject(prepaidcardList).toString()));
			JSONObject obj = new JSONObject();
			obj.putAll(map);
			String result = Call.getCall().exCreatePrepaidcard(obj.toString());
			obj = JSONObject.fromObject(result);
			if(obj.get("success").toString().equals("true")){
				for(T_prepaidcard card : prepaidcardList){
					prepaidcardService.addPrepaidcard(card);
				}
			}
			this.jsonOut("{'success':'true'}");
		} catch (Exception e) {
			try {
				this.jsonOut("{'success':'false'}");
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		return NONE;
	}

	/**
	 * 
	 * 跳转到修改页面
	 * 
	 * @return
	 */
	public String toUpdate() {
		prepaidcard = prepaidcardService.getOnePrepaidcard(id);
		return "update_view";
	}

	/**
	 * 
	 * 修改
	 * 
	 * @return
	 */
	public String update() {
		prepaidcardService.updatePrepaidcard(prepaidcard);
		return "refresh";
	}

	/**
	 * 
	 * 删除
	 * 
	 * @return
	 */
	public String del() {
		prepaidcard = new T_prepaidcard();
		prepaidcard.setId(Integer.parseInt(id));
		prepaidcardService.delPrepaidcard(prepaidcard);
		return "refresh";
	}
	
	
	/**
	 * 
	 * 代理商账户充值列表
	 * @return
	 */
	public String agentRechargeList(){
		T_agentaccountinfo user = this.getSysUser();
		if(prepaidcard == null){
			prepaidcard = new T_prepaidcard();
		}
		prepaidcard.setUseuser(user.getUsername());
		
		int firstResult = (currentPage - 1) * pageSize;
		int maxResults = pageSize;
		Page page = new Page(prepaidcardService.getPageCount(prepaidcard));
		page.setCurrentPage(firstResult / maxResults + 1);

		prepaidcard.setStart_row(page.getStartRow());
		prepaidcard.setEnd_row(page.getStartRow() + pageSize);
		
		super.setPageInfo(page);

		prepaidcardList = prepaidcardService.getPrepaidcardByPager(prepaidcard);
		
		agentaccountinfoList = agentaccountinfoService.getAgentaccountinfoByList(new T_agentaccountinfo());
		
		List<T_prepaidcard>  list = prepaidcardService.getPrepaidcardByCount(prepaidcard);
		if(list.size()>0){
			T_prepaidcard card = list.get(0);
			prepaidcard.setCard_num(card.getCard_num());
			prepaidcard.setCard_money(card.getCard_money());
		}else{
			prepaidcard.setCard_num("0");
			prepaidcard.setCard_money("0");
		}
		return "agentRechargeList_view";
		
		
	}
	

	/**
	 * 
	 * 跳转到代理商账户充值页面
	 * @return
	 */
	public String toAgentRecharge(){
		return "toAgentRecharge_view";
	}
	
	
	/**
	 * 
	 * 代理商账户充值
	 * @return
	 */
	public String agentRecharge(){
		boolean isOk = false;
		String result = null;
		prepaidcardList = prepaidcardService.getPrepaidcardByList(prepaidcard);
		if(prepaidcardList.size() > 0){
			prepaidcard = prepaidcardList.get(0);
			if(prepaidcard.getCardstatus() == 0){
				T_agentaccountinfo user = this.getSysUser();
				
				T_agentaccountinfo agent = new T_agentaccountinfo();
				agent.setUsername(user.getUsername());
				agentaccountinfoList = agentaccountinfoService.getAgentaccountinfoByList(agent);
				agent = agentaccountinfoList.get(0);
				
				if(user.getAgentlevel().equals(prepaidcard.getCardunit())
						&& ((user.getAgentlevel()==2 &&agent.getField4().equals(prepaidcard.getCreateuser())) 
								||(user.getAgentlevel()==1 && prepaidcard.getCreateuser().equals("admin")))){
					prepaidcard.setUseuser(agent.getUsername());
					prepaidcard.setUsedate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
					
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("user", Call.setCallUser(user));
					map.put("data", JsonUtil.jsonStringDelEmpty(JSONObject.fromObject(prepaidcard).toString()));
					JSONObject obj = new JSONObject();
					obj.putAll(map);
					String serverResult = Call.getCall().exAgentRecharge(obj.toString());
					obj = JSONObject.fromObject(serverResult);
					if(obj.get("success").toString().equals("true")){
						prepaidcard.setUsebeforemoney(agent.getAccountmoney());
						prepaidcard.setUseaftermoney(agent.getAccountmoney() + prepaidcard.getCardmoney());
						prepaidcard.setCardstatus(1);
						isOk = true;
						result = "充值成功，当前余额为："+agent.getAccountmoney() + prepaidcard.getCardmoney()+" 元！";
						prepaidcardService.updatePrepaidcard(prepaidcard);
						agent.setAccountmoney(agent.getAccountmoney()+prepaidcard.getCardmoney());
						agentaccountinfoService.updateAgentaccountinfo(agent);
					}else{
						result = obj.get("data").toString();
					}
				}else{
					result = "充值失败，你无权使用该卡！";
				}	
			}else{
				result = "充值失败，该充值卡已使用请勿重复使用！";
			}
		}else {
			result = "充值失败，充值卡卡号或者密码输入错误！";
		}
		try {
			this.jsonOut("{'success':'"+isOk+"',data : '"+result+"'}");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return NONE;
	}

	public PrepaidcardService getPrepaidcardService() {
		return prepaidcardService;
	}

	public void setPrepaidcardService(PrepaidcardService prepaidcardService) {
		this.prepaidcardService = prepaidcardService;
	}

	public T_prepaidcard getPrepaidcard() {
		return prepaidcard;
	}

	public void setPrepaidcard(T_prepaidcard prepaidcard) {
		this.prepaidcard = prepaidcard;
	}

	public List<T_prepaidcard> getPrepaidcardList() {
		return prepaidcardList;
	}

	public void setPrepaidcardList(List<T_prepaidcard> prepaidcardList) {
		this.prepaidcardList = prepaidcardList;
	}

	public String getErrorMsg() {
		return errorMsg;
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

	public int getParame() {
		return parame;
	}

	public void setParame(int parame) {
		this.parame = parame;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}


	public AgentaccountinfoService getAgentaccountinfoService() {
		return agentaccountinfoService;
	}


	public void setAgentaccountinfoService(
			AgentaccountinfoService agentaccountinfoService) {
		this.agentaccountinfoService = agentaccountinfoService;
	}


	public List<T_agentaccountinfo> getAgentaccountinfoList() {
		return agentaccountinfoList;
	}


	public void setAgentaccountinfoList(
			List<T_agentaccountinfo> agentaccountinfoList) {
		this.agentaccountinfoList = agentaccountinfoList;
	}
	
}
